// 账号验证
function userConfirm() {
    var usertext = $('#username').val();
    //手机号或邮箱正则表达式(regexp)
    var reg = /^1[3-9]\d{9}$/;
    if (!reg.test(usertext)) {
        $('#username').css('border', '1px solid red');
    } else {
        $('#username').css('border', '1px solid green');
    }
}


// 密码验证
function passConfirm() {
    var passinput = $('#passwd').val();
    //密码 
    var reg = /^[A-Z][a-zA-Z0-9]{5,15}$/;
    //判断
    if (reg.test(passinput)) {
        $('#passwd').css('border', '1px solid green');
    } else {
        $('#passwd').css('border', '1px solid red');
    }
}



//事件
$('#username').on('keyup', function () {
    userConfirm();
});
$('#passwd').on('keyup', function () {
    passConfirm();
});
//返回上级菜单
$('#back').click(function () {
    history.back();
});
//密码显示隐藏
$('.eye i').click(function () {
    if ($('#passwd').attr('type') == "password") {
        $('#passwd').attr('type', 'text');
        $(this).css('background', 'url(../img/login/eyes-open.png) no-repeat center')
        $(this).css('background-size', 'contain');
    } else {
        $('#passwd').attr('type', 'password');
        $(this).css('background', 'url(../img/login/eyes.png) no-repeat center')
        $(this).css('background-size', 'contain');
    }

});

//获取本地数据
function getData(name) {
    var local = localStorage.getItem(name);
    console.log(local);
    if (local != null) {
        return JSON.parse(local);
    } else {
        return [];
    }
}

//添加本地数据
function saveData(data) {
    localStorage.setItem('login', JSON.stringify(data));

}

//登录点击按钮
$('#btn').on("click", function (e) {
    e.originalEvent.preventDefault();

    // 获取本地数据
    var local = getData('resigter');
    console.log(local);
    //获取输入框数据
    var userval = $("#username").val();
    var passval = $("#passwd").val();

    //新对象
    var obj = {};
    obj.user = userval;
    obj.pass = passval;

    if (userval != "" && passval != "") {
        if (local.length != 0) {
            for (var i = 0; i < local.length; i++) {
                if (local[i].user == userval && local[i].pass == passval) {
                    alert("登录成功");
                    saveData(obj);
                    location.assign("index.html");
                    break;
                }
                //*** 比较妙的用法*/
                if (i == local.length - 1) {
                    alert("用户名或密码错误");
                }
            }
        } else {
            alert('本地注册数据为空');
            location.assign("resigter.html");
        }

    } else {
        alert("请输入账号或密码");
    }

});